package com.company.woye.taiorcheckquestion;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.company.woye.R;
import com.company.woye.base.BaseActivity;
import com.company.woye.checkmakesure.bean.SuccessBean;
import com.company.woye.internet.KjjPHCallBack;
import com.company.woye.taiorcheckquestion.adapter.QuestonListviewAdapter;
import com.company.woye.taiorcheckquestion.bean.Commit;
import com.company.woye.taiorcheckquestion.bean.CommitDetail;
import com.company.woye.taiorcheckquestion.bean.CommitMaster;
import com.company.woye.taiorcheckquestion.bean.QuestionBean;
import com.company.woye.utils.Constans;
import com.company.woye.utils.Toaster;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

public class ChangeQuestionNumActivity extends BaseActivity {


    List<QuestionBean.DataBean> list = new ArrayList<>();
    QuestonListviewAdapter adapter;
    @BindView(R.id.iv_on_package_back)
    ImageView ivOnPackageBack;
    @BindView(R.id.tv_on_package_back)
    TextView tvOnPackageBack;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_refresh)
    TextView tvRefresh;
    @BindView(R.id.tv_LSX)
    TextView tv_part;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.lv_detail)
    ListView lvDetail;
    @BindView(R.id.ll_on_package_summary)
    LinearLayout llOnPackageSummary;
    @BindView(R.id.rl)
    RelativeLayout rl;


    private String cjName, txm, bmName, bw, cm, tagAttr, token, lsx,inputMan,inputId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_change_question_num;
    }

    @Override
    public void initView() {
        adapter = new QuestonListviewAdapter(this, list);
        lvDetail.setAdapter(adapter);


        cjName = getIntent().getStringExtra("cjName");
        txm = getIntent().getStringExtra("txm");
        bmName = getIntent().getStringExtra("bmName");
        bw = getIntent().getStringExtra("bw");
        cm = getIntent().getStringExtra("cm");
        tagAttr = getIntent().getStringExtra("tagAttr");
        token = getIntent().getStringExtra("token");
        lsx = getIntent().getStringExtra("lsx");
        inputId = getIntent().getStringExtra("inputId");
        inputMan = getIntent().getStringExtra("inputMan");
        lsx = getIntent().getStringExtra("lsx");
        tv_part.setText(bw);

        getDetail(cjName, txm, bmName, token, bw, cm, tagAttr);

    }

    private void getDetail(String cjName, String txm, String bmName, String token, String bw, String cm, String tagAttr) {
        JSONObject params = new JSONObject();
        try {
            params.put("cjName", cjName);
            params.put("txm", txm);
            params.put("bmName", bmName);
            params.put("token", token);
            params.put("bw", bw);
            params.put("cm", cm);
            params.put("tagAttr", tagAttr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("提交数据", params.toString());
        Log.e("访问地址", Constans.IP + "szapi/third/BdMlcj/listCjypmlcdjy");
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json"))
                .content(params.toString()).url(Constans.IP + "szapi/third/BdMlcj/listCjypmlcdjy").build().execute(new KjjPHCallBack<QuestionBean>(QuestionBean.class, ChangeQuestionNumActivity.this, true) {
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(QuestionBean response, int id) {
                Toaster.showLong(ChangeQuestionNumActivity.this, response.getMsg());
                if (response.getMsg().equals("success")) {
                    //填充数据
                    List<QuestionBean.DataBean> data = response.getData();
                    if (data.size() > 0) {
                        list.clear();
                        list.addAll(data);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @OnClick({R.id.iv_on_package_back, R.id.tv_on_package_back, R.id.tv_refresh, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_on_package_back:
            case R.id.tv_on_package_back:
                finish();
                break;
            //提交
            case R.id.tv_commit:
                if (!TextUtils.isEmpty(tv_part.getText().toString())) {
                    if (list != null && list.size() > 0) {
                        String bbm = bmName.substring(0, 4);
                        String cjname = cjName;
                        String bwName = bw;
                        String cma = cm;
                        String lsxa = lsx;
                        String txma = txm;
                        CommitDetail commitDetail;
                        List<CommitDetail> zhiCjypcsTagSubList = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            commitDetail = new CommitDetail(list.get(i).getTagCode(), list.get(i).getTagName(), list.get(i).getTagCount());
                            zhiCjypcsTagSubList.add(commitDetail);
                        }
                        Commit commit = new Commit(token,bbm, bmName, cjname, bwName, cma, inputId, inputMan, lsxa, txma, zhiCjypcsTagSubList);
                        Log.e("提交数据", new Gson().toJson(commit));
                        commitInfo(new Gson().toJson(commit));
                    } else {
                        Toast.makeText(ChangeQuestionNumActivity.this, "没有疵点信息", Toast.LENGTH_LONG).show();
                    }

                } else {
                    Toast.makeText(ChangeQuestionNumActivity.this, "没有部位信息", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.tv_refresh:
                sendBroadcast(new Intent("finish"));
                finish();
                break;
        }
    }

    private void commitInfo(String json) {
        Log.e("提交数据", json);
        Log.e("访问地址", Constans.IP + "szapi/third/BdMlcj/saveCjypmlcdjy");
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json"))
                .content(json).url(Constans.IP + "szapi/third/BdMlcj/saveCjypmlcdjy").build().execute(new KjjPHCallBack<SuccessBean>(SuccessBean.class, ChangeQuestionNumActivity.this, true) {
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(SuccessBean response, int id) {
                Toaster.showLong(ChangeQuestionNumActivity.this, response.getMsg());
                if (response.getMsg().equals("success")){
                    finish();
                }
            }
        });
    }
}
